#include<iostream>
using namespace std;
int arr[100];
int brr[100];
int main()
{
	int z = -1;
	for (int i = 'a'; i <= 'a' + 26; i++)
	{
		arr[i] = z--;
	}
	z = 1;
	for (int i = 'A'; i <= 'A' + 26; i++)
	{
		brr[i] = z++;
	}
	int n;
	cin >> n;
	while (n--)
	{
		char x;
		int y;
		getchar();
		cin >> x >> y;
		int ans = 0;
		if (x >= 'a' && x <= 'z') {
			ans = arr[x] + y;
		}
		else {
			ans = brr[x] + y;
		}
		cout << ans << endl;
	}
	return 0;
}